﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        private String direction = "right";
        private int pomme =0;

        public Form1()
        {
            InitializeComponent();

        }

     

        private void button1_Click(object sender, EventArgs e)
        {
            Debug.WriteLine("Before Message");
            MessageBox.Show("message");
            Debug.WriteLine("After Message");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Snake snake = new Snake();
            this.MoveSnake(snake);
            //this.Draw(Snake.head);

            if (this.pomme == 0)
            {
                Random random = new Random();
                int randomX = random.Next(1, 156);
                int randomY = random.Next(1, 112);
                randomX=randomX*5;
                randomY = randomY * 5;
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up && !this.direction.Equals("bottom"))
            {
                this.direction = "top";
            }
            if (e.KeyCode == Keys.Down && !this.direction.Equals("top"))
            {
                this.direction = "bottom";
            }
            if (e.KeyCode == Keys.Left && !this.direction.Equals("right"))
            {
                this.direction = "left";
            }
            if (e.KeyCode == Keys.Right && !this.direction.Equals("left"))
            {
                this.direction = "right";
            }
        }

        private void MoveSnake(Snake snake)
        {
            if (this.direction.Equals("left"))
            {
                this.pictureBox1.Left -= 5;
                if (this.pictureBox1.Left < 5)
                {
                    this.pictureBox1.Left = 780;
                }
            }
            if (this.direction.Equals("right"))
            {
                this.pictureBox1.Left += 5;
                if (this.pictureBox1.Left > 780)
                {
                    this.pictureBox1.Left = 5;
                }
            }
            if (this.direction.Equals("top"))
            {
                this.pictureBox1.Top -= 5;
                if (this.pictureBox1.Top < 5)
                {
                    this.pictureBox1.Top = 560;
                }
            }
            if (this.direction.Equals("bottom"))
            {
                this.pictureBox1.Top += 5;
                if (this.pictureBox1.Top > 560)
                {
                    this.pictureBox1.Top = 5;
                }
            }
        }
        private void Draw(SnakeNode head) { 
            SnakeNode node = head;
            PictureBox[] SnakeBits = new PictureBox[160*120];
            int i = 0;
            while (node.next != null) {
                SnakeBits[i] = new PictureBox();
            //Shapes[i].Name = "ItemNum_" + i.ToString();
                SnakeBits[i].Location = new Point(node.x,node.y);
                SnakeBits[i].Size = new Size(5, 5);
                SnakeBits[i].BackColor = Color.Green;
            //Shapes[i].Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
                SnakeBits[i].Visible = true;
                this.Controls.Add(SnakeBits[i]);
                node = node.next;
                i++;
            }
        }
    }
}